

import com.alibaba.druid.pool.DruidDataSource;
import io.seata.rm.datasource.DataSourceProxy;
import io.seata.rm.datasource.xa.DataSourceProxyXA;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

/**
 * 数据源代理
 * @author wangzhongxiang
 */
 @Configuration
 public class DataSourceConfiguration {

     @Bean
     @ConfigurationProperties(prefix = "spring.datasource")
     public DruidDataSource druidDataSource() {
         return new DruidDataSource();
     }

     @Primary
     @Bean("dataSourceProxy")
     public DataSource dataSource(DataSource druidDataSource) {
         // [AT mode]
         return new DataSourceProxy(druidDataSource);

         // [XA mode]
         // return new DataSourceProxyXA(druidDataSource);
     }

     @Bean("jdbcTemplate")
     public JdbcTemplate jdbcTemplate(DataSource dataSourceProxy) {
         return new JdbcTemplate(dataSourceProxy);
     }

     @Bean
     public PlatformTransactionManager txManager(DataSource dataSourceProxy) {
         return new DataSourceTransactionManager(dataSourceProxy);
     }


 }
